package com.easylinkin.linkappapi.investigation.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.investigation.entity.InvestigationTaskDetail;
import com.easylinkin.linkappapi.investigation.mapper.InvestigationTaskDetailMapper;
import com.easylinkin.linkappapi.investigation.service.InvestigationTaskDetailService;
import com.easylinkin.linkappapi.openapi.util.HttpClientUtil;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 排查任务详情 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2021-04-16
 */
@Service
public class InvestigationTaskDetailServiceImpl extends ServiceImpl<InvestigationTaskDetailMapper, InvestigationTaskDetail> implements InvestigationTaskDetailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(InvestigationTaskDetailServiceImpl.class);

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;
    @Value("${ipservice.url}")
    private String ipserviceUrl;
    @Value("${ipservice.receiveImage}")
    private String receiveImage;
    

    /**
     * 分页查询
     *
     * @author chenkaixuan
     * @since 2021-04-26
     */
    @Override
    public IPage<InvestigationTaskDetail> getInvestigationTaskDetailPage(Page page, String tenantId) {
        QueryWrapper<InvestigationTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("deal_time", "user_name", "area_path", "investigation_type_name", "view_status", "tenant_id");
        if (StringUtils.isEmpty(tenantId)) {
            tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        }
        queryWrapper.eq("tenant_id", tenantId);
        queryWrapper.eq("deal_status", 1);
        queryWrapper.orderByDesc("modify_time");
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 按ID查询
     *
     * @author chenkaixuan
     * @since 2021-04-26
     */
    @Override
    public List<InvestigationTaskDetail> getInvestigationTaskDetailById(Set<Long> ids) {
        QueryWrapper<InvestigationTaskDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("deal_time", "user_name", "area_path", "investigation_type_name", "view_status", "tenant_id");
        queryWrapper.in("id", ids);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean sendFacePicture(String facePicture, String sessionId) {
        HttpPost httpPost = new HttpPost(String.format("%s/%s", ipserviceUrl, receiveImage));
        Map<String, Object> map = new HashMap<>();
        map.put("deviceID", linkappUserContextProducer.getNotNullCurrent().getUsername());
        map.put("requestID", sessionId);
        map.put("faceOfClock", 1);
        map.put("image", facePicture);
        StringEntity se = null;
        try {
            se = new StringEntity(JSON.toJSONString(map));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            LOGGER.error("sendFacePicture--failure:", e);
        }
        LOGGER.info("sendFacePicture-url参数是: " + httpPost.getURI());
        LOGGER.info("sendFacePicture-参数是: " + JSON.toJSONString(map));
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        return str != null && "".equals(str);
    }
}
